package com.atguigu.springcloud.service;

import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.concurrent.TimeUnit;

/**
 * @auther zzyy
 * @create 2020-02-20 11:11
 */
@Service
public class PaymentService {

    /**
     * @HystrixProperty(name = "circuitBreaker.enabled",value = "true" 表示开启熔断
     * @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000") 在10秒钟之内
     * @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10")   请求10次
     * @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")  里面错了6次的话发生熔断
     * 熔断之后将全部降级处理,在过一段时间(默认是5秒),在慢慢的恢复
     */
    @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),// 是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),// 请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), // 时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),// 失败率达到多少后跳闸
    })
    public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
        if (id < 0) {
            throw new RuntimeException("******id 不能负数");
        }
        String serialNumber = IdUtil.simpleUUID();

        return Thread.currentThread().getName() + "\t" + "调用成功，流水号: " + serialNumber;
    }

    public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id) {
        return "id 不能负数，请稍后再试，/(ㄒoㄒ)/~~   id: " + id;
    }

    /**
     *     HystrixCommand 一些配置信息
     *     @HystrixCommand(
     *             commandProperties ={
     *                     //请求超时时间
     *                     @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value ="3000" ),
     *                     //统计窗口内的定义
     *                     @HystrixProperty(name =
     *                             "metrics.rollingStats.timeInMilliseconds",value = "8000"),
     *                     //最小请求数
     *                     @HystrixProperty(name =
     *                             "circuitBreaker.requestVolumeThreshold",value = "2"),
     *                     //错误的请求比例
     *                     @HystrixProperty(name =
     *                             "circuitBreaker.errorThresholdPercentage",value = "50"),
     *                     //自我修复的活窗口时长
     *                     @HystrixProperty(name =
     *                             "circuitBreaker.sleepWindowInMilliseconds",value = "3000")
     *             }
     *             ,fallbackMethod ="myFallBack" //回退方法
     *             //线程池标识，要标识为唯一
     *             ,threadPoolKey = "findRibbonResumeOpenStateTimeout"
     *             //线程池属性
     *             ,threadPoolProperties = {
     *                     //核心纯种
     *                     @HystrixProperty(name = "coreSize",value ="1" ),
     *             //最大等待队列长度
     *             @HystrixProperty(name = "maxQueueSize",value ="20" )
     *     }
     *     )
     */

    /**
     * Hystrix舱壁模式（线程池隔离策略）
     *
     * @HystrixCommand
     * 如果不进⾏任何设置，所有熔断⽅法使⽤⼀个Hystrix线程池（10个线程），那么这样的话会导致问
     * 题，这个问题并不是扇出链路微服务不可⽤导致的，⽽是我们的线程机制导致的，如果⽅法A的请求把
     * 10个线程都⽤了，⽅法2请求处理的时候压根都没法去访问B，因为没有线程可⽤，并不是B服务不可
     * ⽤。
     *
     *     @HystrixCommand(
     *             commandProperties ={
     *                     @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value ="3000" )
     *             }
     *             ,fallbackMethod ="myFallBack" //回退方法
     *             //线程池标识，要标识为唯一
     *             ,threadPoolKey = "findRibbonResumeOpenStateTimeout"
     *             //线程池属性
     *             ,threadPoolProperties = {
     *                     //核心纯种
     *                     @HystrixProperty(name = "coreSize",value ="1" ),
     *             //最大等待队列长度
     *             @HystrixProperty(name = "maxQueueSize",value ="20" )
     *     }
     *     )
     *
     *
     */

}
